package controllers.sys;

import com.alibaba.fastjson.JSONObject;
import controllers.BaseController;
import models.sys.AdminUser;
import packaged.SessionManager;
import play.Play;

import static controllers.sys.Secure.login;
import static controllers.sys.Secure.logout;

/**
 * @author deekian
 */
abstract class BaseSystemController extends BaseController{
    private static JSONObject getLoginUserInfo() {
        return SessionManager.getUserInfo();
    }

    static AdminUser getLoginUser(){
        AdminUser user = AdminUser.findByInfo(getLoginUserInfo());
        if (user == null){
            logout();
            flash.put("url", "GET".equals(request.method) ? request.url : Play.ctxPath + "/"); // seems a good default
            login();
        }
        return user;
    }
}
